#!/bin/sh

mkdir opk/
#git pull
git fetch --all
git merge upstream/FunKey
git merge upstream2/master
./submodule_update.sh
./configure --platform=generic --sound-drivers=sdl
make clean
make
/opt/FunKey-sdk/bin/arm-funkey-linux-musleabihf-strip PicoDrive
cp PicoDrive opk/
cp icons/picodrive.png opk/
cp AUTHORS COPYING README opk/
cp picodrive.sh opk/

# https://unix.stackexchange.com/questions/219268/how-to-add-new-lines-when-using-echo
print()
	case    ${IFS- } in
	(\ *)   printf  %b\\n "$*";;
	(*)     IFS=\ $IFS
	printf  %b\\n "$*"
	IFS=${IFS#?}
esac

# Create GMenu2X entry file plus other things

print '[Desktop Entry]
Name=PicoDrive
Comment=SG/SMS/GG/MD/MCD/32X emulator
Exec=picodrive.sh %f
Icon=picodrive
Categories=emulators
SelectorFilter=zip,bin,md,smd,sg,sms,gg,32x,cue,cso,chd' > opk/picodrive.funkey-s.desktop

rm -f *.opk
mksquashfs opk/ picodrive_v1.3.2_$(git rev-parse HEAD | cut -c1-7)_funkey-s.opk -all-root -noappend -no-exports -no-xattrs
md5sum opk/PicoDrive
rm -rf opk/

make clean
rm -f cpu/cyclone/PicoDrive.map
